function [newLeftList, newRightList] = moveTCList(action, LeftListItems, LeftSelectedItems, RightListItems, RightSelectedItems)
%MOVETCLIST 在两个测试用例列表之间移动项目
%   此函数用于在左右两个列表框之间移动测试用例，支持单项移动、多项移动和全部移动。
%   常用于测试用例选择界面的交互操作。
%
%语法:
%   [newLeftList, newRightList] = moveTCList(action, LeftListItems, LeftSelectedItems, RightListItems, RightSelectedItems)
%
%输入参数:
%   action - 操作类型（字符串），支持以下值：
%            'RightShift' - 将左侧选中项目移动到右侧
%            'RightShiftAll' - 将左侧所有项目移动到右侧
%            'LeftShift' - 将右侧选中项目移动到左侧
%            'LeftShiftAll' - 将右侧所有项目移动到左侧
%   LeftListItems - 左侧列表的所有项目（字符串元胞数组）
%   LeftSelectedItems - 左侧列表中选中的项目（字符串或字符串元胞数组）
%   RightListItems - 右侧列表的所有项目（字符串元胞数组）
%   RightSelectedItems - 右侧列表中选中的项目（字符串或字符串元胞数组）
%
%输出参数:
%   newLeftList - 操作后的左侧列表项目
%   newRightList - 操作后的右侧列表项目
%
%说明:
%   - 支持特殊提示项目：'List all cases'（左侧）和'List selected case(s)'（右侧）
%   - 当这些提示项目存在时，相应的操作会被跳过
%   - 使用union函数保持项目的唯一性和稳定排序
%   - 自动处理单个字符串和字符串元胞数组的输入格式
%
%示例:
%   % 初始化测试用例列表
%   leftItems = {'Test1', 'Test2', 'Test3'};
%   rightItems = {'List selected case(s)'};
%   
%   % 将Test1移动到右侧
%   [newLeft, newRight] = moveTCList('RightShift', leftItems, {'Test1'}, rightItems, {});
%   
%   % 将所有项目移动到右侧
%   [newLeft, newRight] = moveTCList('RightShiftAll', leftItems, {}, rightItems, {});
%
%作者: Alex XU
%版本: 1.0
%日期: 2024
%
%参见:
%   union, intersect
%


% check compliant usage

newRightList = RightListItems;
newLeftList = LeftListItems;
switch action
    case 'RightShift'
        tcList = LeftListItems;
        if strcmp(tcList{1}, 'List all cases')
            return
        end
        % get cases list to be moved to Right
        if ischar(LeftSelectedItems)
            casesToBeMoved = {LeftSelectedItems};
        else
            casesToBeMoved = LeftSelectedItems;
        end
        % get cases list which have been moved to Right
        selectedCases = RightListItems;
        if isempty(selectedCases)||strcmp(selectedCases{1},'List selected case(s)')
            selectedCases = [];
        end
        selectedCases  = union(selectedCases,casesToBeMoved,'stable');
        newRightList = selectedCases;
    case 'RightShiftAll'
        tcList = LeftListItems;
        if strcmp(tcList{1}, 'List all cases')
            return
        end
        newRightList = tcList;
    case 'LeftShift'
        selectedCaseList = RightListItems;
        selectedCases    = RightSelectedItems;
        if ischar(selectedCases)
            selectedCases = {selectedCases};
        end
        % check if no case to be moved
        if isempty(selectedCases) || strcmp(selectedCases{1},'List selected case(s)')
            return;
        end
        % find the index
        [~,~,selectedTCNum] = intersect(selectedCases, selectedCaseList,'stable');
        % remove the seltced case from the list
        selectedCaseList(selectedTCNum) = [];
        if isempty(selectedCaseList)
            selectedCaseList{1} = 'List selected case(s)';
        end
        newRightList = selectedCaseList;
    case 'LeftShiftAll'
        newRightList = {'List selected case(s)'};
    otherwise
        % do nothing
end

end